import { createRouter, createWebHashHistory } from 'vue-router'

import store from '@/store'

const routes = [
  {
    path: '/',
    name: 'index',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/layout'),
    redirect: '/first',
    children: [
      {
        path: '/first',
        name: 'first',
        component: () => import('@/views/firstPage'),
        meta: {
          title: '看板'
        }
      },
      {
        path: '/second',
        name: 'second',
        component: () => import('@/views/second'),
        meta: {
          title: '运营分类'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  console.log(store.state.auth.token, to)
  if (to.path !== '/login') {
    if (!store.state.auth.token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
